package org.mine.design.builder.service.impl;

import org.mine.design.builder.controller.param.RequestParam;
import org.mine.design.builder.data.BaseCost;
import org.mine.design.builder.service.CostCalculateService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BaseCostCalculateServiceImpl implements CostCalculateService {

    @Override
    public int calculate(RequestParam param) {
        String city = param.getCity();
        List<BaseCost> baseCostList = BaseCost.getBaseCostList();
        return baseCostList.stream().filter(source -> source.getCity().equals(city))
                .findFirst()
                .map(source -> {
                    Integer fare = source.getStartingFare();
                    if (param.getKilometre() > source.getStartingKilometre()) {
                        fare += param.getKilometre() - source.getStartingKilometre() * source.getStagePrice();
                    }
                    return fare;
                }).orElse(0);
    }
}
